﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
// 
using Interfaces;
using Corsis.Xhtml;

namespace MyXHTMLConverter
{
    public partial class CorsisXHTMLConverterPlugin : UserControl, IXHTMLConverter
    {
        public CorsisXHTMLConverterPlugin()
        {
            InitializeComponent();
        }

        private IPreparationHost pluginHost = null;
        public IPreparationHost PluginHost
        {
            get
            {
                return pluginHost;
            }
            set
            {
                this.pluginHost = value;
            }
        }

        private string pluginName = "Corsis HTML2XHTML Converter";
        public string PluginName
        {
            get
            {
                return this.pluginName;
            }
        }

        private string pluginDescription = "Convert HTML format file to XHTML format.";

        public string PluginDescription
        {
            get { return pluginDescription; }
            set { pluginDescription = value; }
        }

        public UserControl PluginUserControl
        {
            get
            {
                return this;
            }
        }

        public void Initialize()
        {
        }

        public void PluginDispose()
        {
        }

        public void ConvertToXHML(string filePath, string newFilePath)
        {
            if (File.Exists(filePath))
            {
                // Load the content of a page into string
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                string htmlStr = sr.ReadToEnd();
                // Close streams
                sr.Close();
                fs.Close();
                // Convert html to xhtml
                try
                {
                    string xhtmlString;
                    //xhtmlString = Corsis.Xhtml.Html2Xhtml.RunAsFilter(stdin => stdin.Write(htmlStr)).ReadToEnd();
                    xhtmlString = htmlStr;
                    // Write xhtml to new file
                    fs = new FileStream(newFilePath, FileMode.Create, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.Write(xhtmlString);
                    // Close streams
                    sw.Close();
                    fs.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                }                
            }
            else
            {
                throw new Exception("File does not exist!");
            }
        }
    }
}
